package com.itwk.yygh.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


/**
 * @author wangke
 * @version 1.0.0
 * @Description 全局跨域配置
 * @createTime 2021年08月21日 14:28:00
 */

@Configuration
public class WebCorsConfiguration {
        @Bean
        public CorsFilter corsFilter(){
            CorsConfiguration config = new CorsConfiguration();
            //允许请求的头
            config.addAllowedHeader("*");
            //允许请求的方法
            config.addAllowedMethod("*");
            //是否可以携带cookies 等信息
            config.setAllowCredentials(true);
            //允许跨域访问的地址
            config.addAllowedOrigin("*");
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            //注册 拦截规则，和跨域配置
            source.registerCorsConfiguration("/**",config);

            return  new CorsFilter(source);
    }
}
